package com.etrans.web.gps.business.report.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.Document;
import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.common.MGTemplete;
import com.etrans.web.gps.util.DateUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Service
public class RemindAnalyzesService extends BaseService {

	@SuppressWarnings("unchecked")
	public Map<String, Object> loadChartData(String remindType, String reminder, String startDate, String endDate) {
		Map<String, Object> result = new HashMap<>();
		try {
			DBObject condition = new BasicDBObject();
			HashMap<String, Object> timeMap = new HashMap<>();
			timeMap.put("$gte",DateUtil.getISODate(startDate));
			timeMap.put("$lte",DateUtil.getISODate(endDate));
			condition.put("tips_time", timeMap);
			// 0：短信 1：邮件
			if (remindType != null && !"".equals(remindType)) {
				condition.put("tips_type", Integer.parseInt(remindType));
			} 
			
	/*		 // 0：平台联系人 1：业户联系人 2：从业人员 3：区域联系人 4 ：车辆联系人 if(reminder !=null &&
			if("0".equals(reminder)){
				condition.put("target_type", "平台联系人");
			}else if(reminder !=null && "1".equals(reminder)){
				condition.put("target_type", "业户联系人"); 
			}else if(reminder !=null && "2".equals(reminder)){ 
				condition.put("target_type", "从业人员");
			}else if(reminder !=null && "3".equals(reminder)){
				condition.put("target_type", "区域联系人"); 
			}else if(reminder !=null && "4".equals(reminder)){ 
				condition.put("target_type", "车辆联系人"); 
			}*/
			 
			List<Integer> ySMS = new ArrayList<>();
			List<Integer> yMail = new ArrayList<>();
			List<String> xAxis = new ArrayList<>();

			DBObject initial = new BasicDBObject("count", 0);
			DBObject key = new BasicDBObject("tips_type", 1);
			key.put("target_type", 1);
			String reduce = "function(obj,pre){pre.count++}";
			List<Object> temp = MGTemplete.group("egov", "tips_record", key, condition, initial, reduce, null);
			for (int i = 0; i < temp.size(); i++) {
				Map<String, Object> map =  (Map<String, Object>) temp.get(i);
				String target_type ="";
				int targetType = Integer.parseInt(String.valueOf(map.get("target_type")).split("\\.")[0]);
				if(targetType==0){
					target_type = "平台联系人";
				}else if(targetType==1){
					target_type ="业户联系人";
				}else if(targetType==2){
					target_type ="从业人员";
				}else if(targetType==3){
					target_type ="区域联系人";
				}else if(targetType==4){
					target_type = "车辆联系人";
				}
				if (xAxis.contains(target_type)) {
					//获取List相同的提醒对象的位置
					int index = xAxis.indexOf(target_type);
					String tips_type = String.valueOf(map.get("tips_type"));
					int type =Integer.parseInt(tips_type.split("\\.")[0]);
				
					//把同一提醒对象的统计条数插入到相应位置
					if (type == 0) {
						String str = String.valueOf(map.get("count"));
						String[] s = str.split("\\.");
						ySMS.set(index, Integer.parseInt(s[0]));
					} else {
						String str = String.valueOf(map.get("count"));
						String[] s = str.split("\\.");
						yMail.set(index, Integer.parseInt(s[0]));
					}
				} else {
					xAxis.add(target_type);
					String tips_type = String.valueOf(map.get("tips_type"));
					
					int type =Integer.parseInt(tips_type.split("\\.")[0]);
					if (type == 0) {
						String str = String.valueOf(map.get("count"));
						String[] s = str.split("\\.");
						ySMS.add(Integer.parseInt(s[0]));
						yMail.add(0);
					} else {
						String str = String.valueOf(map.get("count"));
						String[] s = str.split("\\.");
						yMail.add(Integer.parseInt(s[0]));
						ySMS.add(0);
					}
				}
			}
			result.put("ySMS", ySMS);
			result.put("yMail", yMail);
			result.put("xAxis", xAxis);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}

	public List<Document> loadTableData(Map<String, Object> param) {
		HashMap<String, Object> sortMap = new HashMap<>();
		HashMap<String, Object> whereMap = new HashMap<>();
		String remindType = String.valueOf(param.get("remindType"));
		HashMap<String, Object> timeMap = new HashMap<>();
		timeMap.put("$gte", DateUtil.getISODate(String.valueOf(param.get("startDate"))));
		timeMap.put("$lte", DateUtil.getISODate(String.valueOf(param.get("endDate"))));
		String reminder = String.valueOf(param.get("reminder"));
		 
		whereMap.put("tips_time", timeMap);
		// 0：短信 1：邮件
		if (remindType != null && !"".equals(remindType)) {
			whereMap.put("tips_type", Integer.parseInt(remindType));
		}
		if (reminder != null && !"".equals(reminder)) {
			whereMap.put("target_type", Integer.parseInt(reminder));
		}
		return MGTemplete.find("egov", "tips_record", whereMap, sortMap, (int) param.get("page"),(int) param.get("pageSize"));
	}

	public int loadTableDataCount(Map<String, Object> param) {
		HashMap<String, Object> whereMap = new HashMap<>();
		String remindType = String.valueOf(param.get("remindType"));
		HashMap<String, Object> timeMap = new HashMap<>();
		timeMap.put("$gte", DateUtil.getISODate(String.valueOf(param.get("startDate"))));
		timeMap.put("$lte", DateUtil.getISODate(String.valueOf(param.get("endDate"))));
		whereMap.put("tips_time", timeMap);
		String reminder = String.valueOf(param.get("reminder"));
		// 0：短信 1：邮件
		// 0：平台联系人 1：业户联系人 2：从业人员 3：区域联系人 4 ：车辆联系人
		// 0：短信 1：邮件
		if (remindType != null && !"".equals(remindType)) {
			whereMap.put("tips_type", Integer.parseInt(remindType));
		}
		if (reminder != null && !"".equals(reminder)) {
			whereMap.put("target_type", Integer.parseInt(reminder));
		}
		long count = MGTemplete.findCount("egov", "tips_record", whereMap);
		return Integer.parseInt(String.valueOf(count));
	}

}
